#encoding: utf-8
require 'rubygems'
require 'bundler'
require 'bundler/setup'
require 'rake/clean'
require 'flashsdk'
require 'semilla'


##########
# Project name
# This is the name used in generated binaries, and other files. 
# Also the name of the main class.
PROJECT_NAME = "MagicBox"
##########

#Define some names
DEBUG_BIN = "bin/#{PROJECT_NAME}-debug.swf"
RELEASE_BIN = "bin/#{PROJECT_NAME}-release.swf"
MAIN_AS = "src/#{PROJECT_NAME}.as"
TEST_AS = "test-src/TestRunner.as"
TEST_BIN = "bin/#{PROJECT_NAME}-flexunit.swf"
REPORT_PATH = "test-report"
TEST_PORT = 1026

#Files to remove on CLEAN.
CLEAN << FileList["bin/*.swf"] 			#Binaries
CLEAN << FileList["#{REPORT_PATH}/*"] 	#Test results
CLEAN << FileList[TEST_AS] 				#Auto-generated Test Script

##Create directory tasks
directory "bin"
directory REPORT_PATH #Dependency for the test task


# Set some shared config options to the passed compiler.
def config_common_options compiler
  compiler.static_link_runtime_shared_libraries = true
  compiler.source_path << "src"
  compiler.library_path << "lib"
end

##############################
# Debug

# Compile the debug swf
mxmlc DEBUG_BIN do |t|
  t.input = MAIN_AS
  t.debug = true
  config_common_options t
end

desc "Compile a debug version of the project."
task :compile => DEBUG_BIN

desc "Compile and run the debug swf"
flashplayer :run => DEBUG_BIN

##############################
# Release

# Compile the debug swf
mxmlc RELEASE_BIN do |t|
  t.input = MAIN_AS
  t.debug = false
  config_common_options t
end

desc "Compile a release version of the project."
task :compile_release => RELEASE_BIN


##############################
# FLEXUNIT TESTS

# The list of FlexUnit test cases.
test_cases = FileList["test-src/**/*Test.as"]
# FileTask: Generate a script that runs all the test cases.
Semilla::test_runner TEST_AS, test_cases, TEST_PORT

desc "Generate the TestRunner script"
task :testrunner => TEST_AS

# Compile the TestRunner
mxmlc TEST_BIN => TEST_AS do |t|
  t.input = TEST_AS
  t.debug = true
  config_common_options t
  t.library_path << "lib/flexunit4"
end

task :compile_flexunit => TEST_BIN

# Run the Tests.
desc "Test with FlexUnit"
Semilla::flex_unit :test => [:compile_flexunit, REPORT_PATH] do |t|
  t.serverPort = TEST_PORT
  t.swf = TEST_BIN
  t.reportpath = REPORT_PATH
  t.timeout = 15 #seconds
end


##############################
# SWC

compc "bin/#{PROJECT_NAME}.swc" do |t|
  t.input_class = "#{PROJECT_NAME}"
  config_common_options t
end

desc "Compile the SWC file"
task :swc => 'bin/#{PROJECT_NAME}.swc'

##############################
# DOC

desc "Generate documentation at doc/"
asdoc 'doc' do |t|
  t.doc_sources << "src"
  #t.exclude_sources << "src/#{PROJECT_NAME}Runner.as"
end

##############################
# DEFAULT, compile the debug & release versions, run tests.
task :default => [:compile, :compile_release, :test]


